const {ccclass, property} = cc._decorator


@ccclass
export default class AppEnv extends cc.Component
{
    // 运行平台，web,android, ios, 微信等
    platform:string = "unknown";
    // 操作系统版本 android 9
    os:string = "unknown";
    osVersion:string = "unknown";
    osMainVersion:number = 0;

    browserType: string = "unknown";
    browserVersion: string = "unknown";
    windowPixelResolution: cc.Vec2 = cc.Vec2.ZERO;

    // 设备网络类型
    networkType:number = 0;
    // 系统语言
    languageCode: string = "en";

    // 设备电量
    batteryLevel:number = 1;
    cpuInfo:string = "unknown";
    memoryInfo:string = "unknown";

    onLoad()
    {
        cc.log("AppEnv|onLoad");

        this.platform = this.getPlatFormName(cc.sys.platform);
        this.os = cc.sys.os;
        this.osVersion = cc.sys.osVersion;
        this.osMainVersion = cc.sys.osMainVersion;

        if(typeof(cc.sys.browserType) === "string")
        {
            this.browserType = cc.sys.browserType;
        }
        if(typeof(cc.sys.browserVersion) === "string")
        {
            this.browserVersion = cc.sys.browserVersion;
        }


        let size = cc.sys.windowPixelResolution;
        this.windowPixelResolution = cc.v2(size.width, size.height);
        
        this.networkType = cc.sys.getNetworkType();
        this.languageCode = cc.sys.languageCode;

        this.batteryLevel = cc.sys.getBatteryLevel();

        this.dump();
    }

    getPlatFormName(platform:number) : string
    {
        let result = "";
        switch(platform)
        {
            case cc.sys.UNKNOWN:result = "unknown"; break;
            case cc.sys.WIN32: result = "win32"; break;
            case cc.sys.MACOS: result = "macos"; break;
            case cc.sys.ANDROID: result = "android"; break;
            case cc.sys.IPHONE: result = "iphone"; break;
            case cc.sys.IPAD: result = "ipad"; break;
            case cc.sys.MOBILE_BROWSER: result = "mobile_browser"; break;
            case cc.sys.DESKTOP_BROWSER: result = "desktop_browser"; break;
            case cc.sys.WECHAT_GAME: result = "wechat_game"; break;
            case cc.sys.DESKTOP_BROWSER: result = "desktop_browser"; break;
            case cc.sys.DESKTOP_BROWSER: result = "desktop_browser"; break;
            default: result = "unknown";break;
        }
        return result;
    }

    getNetworkName(networkType): string
    {
        let result = "";
        switch(networkType)
        {
            case cc.sys.NetworkType.NONE:  result = "none"; break;
            case cc.sys.NetworkType.LAN: result = "lan"; break;
            case cc.sys.NetworkType.WWAN: result = "wwan"; break;
            default: result = "unknown"; break;
        }
        return result;
    }


    dump()
    {
        cc.log("platform:" + this.platform);
        cc.log("os:" + this.os + "|" + this.osVersion + "|" + this.osMainVersion);
        cc.log("browser:" + this.browserType + "|" + this.browserVersion);
        cc.log("pixel resolution:" + this.windowPixelResolution.toString());
        cc.log("network:" + this.getNetworkName(this.networkType));
        cc.log("language:" + this.languageCode);
        cc.log("battery leve:" + this.batteryLevel);
    }
}